/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Negocio;

import DAO.ResourcesDAO;
import Entidades.ResourceEnt;
import java.sql.SQLException;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;

public class CorreoNeg {
    
    ResourcesDAO resource;
    String miCorreo;
    String miContraseña;
    final String servidorSMTP = "smtp.gmail.com";
    final String puertoEnvio = "465";


    public CorreoNeg(String mailReceptor, String asunto,
            String cuerpo) throws SQLException {
       
        List<ResourceEnt> lstReso;
        resource = new ResourcesDAO();
        lstReso = resource.busquedaResources();
        
        
        for (ResourceEnt res : lstReso) {
             
            if(res.getClave().trim().equals("USUARIO_CORREO")){
                 miCorreo = res.getValor();
             }
            
            if(res.getClave().trim().equals("CLAVE_CORREO")){
                 miContraseña = res.getValor();
             }
        }

        Properties props = new Properties();
        props.put("mail.smtp.user", miCorreo);
        props.put("mail.smtp.host", servidorSMTP);
        props.put("mail.smtp.port", puertoEnvio);
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.socketFactory.port", puertoEnvio);
        props.put("mail.smtp.socketFactory.class",
                "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.socketFactory.fallback", "false");

        SecurityManager security = System.getSecurityManager();

        try {
            Authenticator auth = new autentificadorSMTP();
            Session session = Session.getInstance(props, auth);
            

            MimeMessage msg = new MimeMessage(session);
            msg.setText(cuerpo);
            msg.setSubject(asunto);
            msg.setFrom(new InternetAddress(miCorreo));
            msg.addRecipient(Message.RecipientType.TO, new InternetAddress(
                    mailReceptor));
            Transport.send(msg);
        } catch (Exception mex) {
            mex.printStackTrace();
        }

    }

    private class autentificadorSMTP extends javax.mail.Authenticator {
        public PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(miCorreo, miContraseña);
        }
    }

}